﻿$(function() {
	var enalbeGeticode = true;
	$("select").each(function(index, element) {
		zsmm.Dropdown.New($(this));
	});
	if($('#geticode').length > 0) {
		$('#geticode').click(function(e) {
			zsmm.isValidated = true;
			validateInput("mobile");
			validatePhone("mobile");
			if(!zsmm.isValidated) {
				return false;
			}
			var phone = $.trim($('input[name="mobile"]').eq(0).val());
			if(enalbeGeticode) {
				enalbeGeticode = false;
				var i = 60;
				var obj = $(this);
				obj.text(i + '秒后重试').addClass('disabled');
				var intval = setInterval(function() {
					i -= 1;
					obj.text(i + '秒后重试');
					if(i <= 0) {
						clearInterval(intval);
						obj.text('获取验证码').removeClass('disabled');
						enalbeGeticode = true;
						return;
					}
				}, 1000);
			}
		});
	}
	$("#cardnumber").blur(function(e) {
		e.preventDefault();
		var idcard = $.trim($(this).val());
		var type = $("#cardtype").val().toString();
		if(idcard != "") {
			zsmm.isValidated = true;
			validateCardNumber("cardnumber", type);
			if($("#birthday").val() != "") return false;
			if(zsmm.isValidated && (type == "1")) {
				$("#birthday").val(getBirthday(idcard));
			} else if(zsmm.isValidated && (type != "1")) {
				$("#birthday").val(getBirthday());
			}
		}
	});
	$("#mobile").blur(function(e) {
		e.preventDefault();
		var mobile = $.trim($(this).val());
		if(mobile != "") {
			zsmm.isValidated = true;
			validatePhone("mobile");
		}
	});
	$("button[type='submit']").click(function(e) {
		e.preventDefault();
		validation();
	});
});

function getBirthday(idcard) {
	var year, month, day;
	if(typeof(idcard) != "undefined") {
		if(idcard.length == 18) {
			year = idcard.substring(6, 10);
			month = idcard.substring(10, 12);
			day = idcard.substring(12, 14);
		} else if(idcard.length == 15) {
			year = '19' + idcard.substring(6, 8);
			month = idcard.substring(8, 10);
			day = idcard.substring(10, 12);
		}

	} else {
		var date = new Date();
		year = date.getFullYear() - 35;
		month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
		day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	}
	return year + "-" + month + "-" + day;
}

function validation() {
	zsmm.isValidated = true;
	validateInput("name");
	validateSelect("cardtype");
	validateCardNumber("cardnumber", $("#cardtype").val());
	validateInput("cardnumber");
	validateInput("birthday");
	validateInput("duedate");
	validateInput("mobile");
	validatePhone("mobile");
	validateSelect("collection");
	validateSelect("doctor");
	if(zsmm.isValidated) {
		$("form").submit();
	}
}